<template>
  <div>
    <h3>权限管理组件</h3>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator=">">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card>
      <!-- 首行 -->
      <el-row>
        <el-col><el-button type="primary" class="top-btn" @click="showAddCateDialog"> 添加分类</el-button> </el-col>
      </el-row>
      <!-- 表格主体区域 -->
      <tree-table :data="cateList" :columns="columns" :selection-type="false" :expand-type="false" show-index index-text="排序" border :show-row-hover="false">
        <template #isok="scope">
          <i class="el-icon-success" v-if="scope.row.cat_deleted === false" style="color: lightgreen"></i>
          <i class="el-icon-error" v-else style="color: red"></i>
        </template>
        <template slot="order" slot-scope="scope">
          <el-tag size="mini" v-if="scope.row.cat_level == 0" type="success">一级</el-tag>
          <el-tag size="mini" v-else-if="scope.row.cat_level == 1" type="warning">二级</el-tag>
          <el-tag size="mini" v-else type="info">三级</el-tag>
        </template>
        <template #opt="scope">
          <el-button type="primary" icon="el-icon-edit" size="mini" @click="editData(scope.row)">编辑</el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteData(scope.row)">删除</el-button>
        </template>
      </tree-table>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!-- 添加分类对话弹出框 -->
    <el-dialog title="添加分类" :visible.sync="addCateDialogVisible" width="50%" @close="addCateDialogClosed">
      <!-- 添加分类表单 -->
      <el-form :model="addCateForm" :rules="addCateFormRules" ref="addCateFormRuleForm" label-width="100px">
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addCateForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类">
          <el-cascader v-model="addCateForm.cat_pid" :options="parentCateList" :props="cascaderProps" @change="handleChange" clearable></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addCateDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCate">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 修改参数或属性对话框 -->
    <el-dialog title="编辑分类" :visible.sync="editDialogVisible" width="50%" @close="editDialogClosed">
      <!-- 添加表单 -->
      <el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="100px">
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="editForm.cat_name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editParams">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      cateList: [],
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      total: 0,
      //   tree-table配置
      columns: [
        { label: '分类名称', prop: 'cat_name' },
        // 自定义模板列
        { label: '是否有效', type: 'template', template: 'isok' },
        { label: '排序', type: 'template', template: 'order' },
        { label: '操作', type: 'template', template: 'opt' }
      ],
      //   新增弹出框
      addCateDialogVisible: false,
      //   校验规则
      addCateFormRules: {
        cat_name: [
          { required: true, message: '请输入分类名', trigger: 'blur' },
          {
            min: 2,
            max: 10,
            message: '长度在 3 到 10 个字符',
            trigger: 'blur'
          }
        ]
      },
      addCateForm: {
        cat_name: '',
        cat_pid: 0,
        cat_level: 0
      },
      // 保存1,2级父级分类的列表
      parentCateList: [],
      //   级联选择框配置对象
      cascaderProps: {
        checkStrictly: true,
        expandTrigger: 'hover',
        value: 'cat_id',
        label: 'cat_name'
      },
      // 编辑分类弹出框
      editDialogVisible: false,
      // 编辑数据
      editForm: {
        cat_name: '',
        cat_id: 0
      },
      // 验证规则
      editFormRules: {
        cat_name: [{ required: true, message: '请输入名称', trigger: 'blur' }]
      }
    }
  },
  created() {
    //   开局得分类
    this.getCateList()
  },
  methods: {
    //   获取整体分类数据
    async getCateList() {
      //   const { data: res } = await this.$axios.get('categories', {
      //     params: this.queryInfo,
      //   })
      const { data: res } = await this.$axios({
        url: 'categories',
        method: 'GET',
        params: this.queryInfo
      })
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.cateList = res.data.result
      this.total = res.data.total
    },
    // pagesize改变
    handleSizeChange(newSzie) {
      this.queryInfo.pagesize = newSzie
      this.getCateList()
    },
    // page页码改变
    handleCurrentChange(newpage) {
      this.queryInfo.pagenum = newpage
      this.getCateList()
    },
    // 新增分类
    showAddCateDialog() {
      this.addCateDialogVisible = true
      // 先获取1-2级分类列表
      this.getParentCateList()
    },
    // 关闭弹出框之后DO
    addCateDialogClosed() {
      this.$refs.addCateFormRuleForm.resetFields()
      this.addCateForm.cat_pid = 0
    },
    // 获取父级分类列表
    async getParentCateList() {
      const { data: res } = await this.$axios({
        url: 'categories',
        method: 'GET',
        params: { type: 2 }
      })
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.parentCateList = res.data
    },
    handleChange() {
      // 将自身分类层级确定
      this.addCateForm.cat_level = this.addCateForm.cat_pid.length

      this.addCateForm.cat_pid = this.addCateForm.cat_pid[this.addCateForm.cat_pid.length - 1]
    },
    // 点击确定发起请求,新增分类
    async addCate() {
      //   将父类ID确定
      if (this.addCateForm.cat_pid === 0) {
        this.addCateForm.cat_pid = 0
        this.addCateForm.cat_level = 0
      }
      // 发起请求新增列表
      const { data: res } = await this.$axios({
        url: 'categories',
        method: 'POST',
        data: this.addCateForm
      })
      if (res.meta.status !== 201) return this.$message.error(res.meta.msg)
      this.$message.success(res.meta.msg)
      //   关闭弹出层
      this.addCateDialogVisible = false
      //   重新获取分类数据, 刷新页面
      this.getCateList()
    },
    // 关闭弹出框后
    editDialogClosed() {
      this.$refs.editFormRef.resetFields()
    },
    // 编辑按钮
    editData(val) {
      this.editDialogVisible = true
      this.editForm.cat_id = val.cat_id
      this.editForm.cat_name = val.cat_name
    },
    // 编辑确定
    async editParams() {
      const { data: res } = await this.$axios.put(`categories/${this.editForm.cat_id}`, { cat_name: this.editForm.cat_name })
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.$message.success(res.meta.msg)
      this.getCateList()
      this.editDialogVisible = false
    },
    // 删除按钮
    async deleteData(val) {
      try {
        await this.$messagebox.confirm('是否删除该用户?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        const { data: res } = await this.$axios.delete(`categories/${val.cat_id}`)
        if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
        this.$message.success(res.meta.msg)
        if (this.cateList.length === 1) this.queryInfo.pagenum -= 1
        this.getCateList()
      } catch {}
    }
  }
}
</script>

<style scoped>
.top-btn {
  margin-bottom: 20px;
}
</style>
